package com.lxr0860.lxr0860production_test.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import com.lxr0860.lxr0860production_test.R;

public class MyViewOld extends View {

    private Paint xLinePaint;// 坐标轴 轴线 画笔：
    private Paint hLinePaint;// 坐标轴水平内部 虚线画笔
    private Paint titlePaint;// 绘制文本的画笔
    private Paint paint;// 矩形画笔 柱状图的样式信息
    private float[] progress;// 7 条
    private int[] aniProgress;// 实现动画的值
    private final int TRUE = 1;// 在柱状图上显示数字
    private boolean[] showText;
    // 坐标轴左侧的数标
    private String[] ySteps;
    // 坐标轴底部的卫星号码
    private String[] mIndexs;

    private HistogramAnimation ani;

    public MyViewOld(Context context) {
        super(context);
        init(context, null);
    }

    public MyViewOld(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        ySteps = new String[]{"100", "90", "80", "70", "60", "50", "40", "30", "20", "10", "0"};
        showText = new boolean[]{true, true, true, true, true, true, true, true, true, true,
                true, true, true, true, true, true};
        aniProgress = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        ani = new HistogramAnimation();

        xLinePaint = new Paint();
        hLinePaint = new Paint();
        titlePaint = new Paint();
        paint = new Paint();

        xLinePaint.setColor(Color.BLUE);
        hLinePaint.setColor(Color.BLUE);
        titlePaint.setColor(Color.BLUE);
    }

    public void setText(boolean[] text) {

        this.showText = text;
        this.postInvalidate();// 可以子线程 更新视图的方法调用。
    }

    public void setProgress(float[] progress, String[] indexs) {
        this.progress = progress;
        this.mIndexs = indexs;
        // this.invalidate(); //失效的意思。
        // this.postInvalidate(); // 可以子线程 更新视图的方法调用。
        this.startAnimation(ani);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        int width = getWidth() - 10;
        int height = getHeight() - 90;

        // 1 绘制坐标线：startX, startY, stopX, stopY, paint
        int startX = dip2px(getContext(), 50);
        int startY = dip2px(getContext(), 10);
        int stopX = dip2px(getContext(), 50);
        int stopY = dip2px(getContext(), 320);
        canvas.drawLine(50, 10, 50, height, xLinePaint);

        canvas.drawLine(50, height, width - 10, height, xLinePaint);

        // 2 绘制坐标内部的水平线

        int leftHeight = height - 10;// 左侧外周的 需要划分的高度：

        int hPerHeight = leftHeight / 10;// 分成四部分

        hLinePaint.setTextAlign(Align.CENTER);
        for (int i = 0; i < 10; i++) {
            canvas.drawLine(50, 20 + i * hPerHeight, width - 10, 20 + i
                    * hPerHeight, hLinePaint);
        }

        // 3 绘制 Y 周坐标

        titlePaint.setTextAlign(Align.RIGHT);
        titlePaint.setTextSize(18);
        titlePaint.setAntiAlias(true);
        titlePaint.setStyle(Paint.Style.FILL);
        for (int i = 0; i < ySteps.length; i++) {
            canvas.drawText(ySteps[i], 40, 20 + i * hPerHeight, titlePaint);
        }
        if (mIndexs == null) {
            mIndexs = new String[]{"", "", "", "", "", "", "", "", "", "",
                    "", "", "", "", "", ""};
        }
        // 4 绘制 X 周 做坐标
        int xAxisLength = width - 30;
        int columCount = mIndexs.length + 1;
        int step = 40;
//        int step = xAxisLength / columCount;
        for (int i = 0; i < columCount - 1; i++) {
            if (mIndexs[i] != null) {
                canvas.drawText(mIndexs[i], 50 + step * (i + 1), height + 20, titlePaint);
            }

        }

        // 5 绘制矩形
//        Rect rect = new Rect();
//        rect.left = 25;
//        rect.right = rect.left + 25;
        if (aniProgress != null && aniProgress.length > 0) {
            for (int i = 0; i < aniProgress.length; i++) {// 循环遍历将柱状图形画出来
                int value = aniProgress[i];
                paint.setAntiAlias(true);// 抗锯齿效果
                paint.setStyle(Paint.Style.FILL);
                paint.setTextSize(18);// 字体大小
                paint.setColor(Color.parseColor("#56121e"));// 字体颜色
                Rect rect = new Rect();
                rect.left = 40 + step * (i + 1) - 8;
                rect.right = 30 + step * (i + 1) + 20;
                int rh = (int) (leftHeight - leftHeight * (value / 100.0));
                rect.top = rh + 18;//与我的视图横线刚好
                rect.bottom = height;

                Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                        R.drawable.statelite_signal_bg);

                canvas.drawBitmap(bitmap, null, rect, paint);

                if (this.showText[i]) {
                    if (value == -1 || value == 0) {
                        canvas.drawText("", 40 + step * (i + 1) - 9,
                                rh + 10, paint);
                    } else {
                        if (value < 10) {
                            canvas.drawText(" " + value, 40 + step * (i + 1) - 9,
                                    rh + 10, paint);
                        } else {
                            canvas.drawText(value + "", 40 + step * (i + 1) - 9,
                                    rh + 10, paint);
                        }
                    }
                }

            }
        }

    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 集成animation的一个动画类
     *
     * @author
     */
    private class HistogramAnimation extends Animation {
        @Override
        protected void applyTransformation(float interpolatedTime,
                                           Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            if (interpolatedTime < 1.0f) {
                for (int i = 0; i < aniProgress.length; i++) {
                    aniProgress[i] = (int) (progress[i] * interpolatedTime);
                }
            } else {
                for (int i = 0; i < aniProgress.length; i++) {
                    aniProgress[i] = (int) progress[i];
                }
            }
            postInvalidate();
        }
    }
}
